<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>首页</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
	<div class="layui-tab layui-tab-card" lay-filter="tabDemo">

		<ul class="layui-tab-title">
			<li class="layui-this" lay-id="heikeyunTab">磁力解析</li>
			<li>磁力搜索</li>
		</ul>

		<div class="layui-tab-content">

			<!-- 磁力解析 -->
			<div class="layui-tab-item layui-show">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<label class="layui-form-label">磁力链接</label>
						<div class="layui-input-block">
							<input type="text" id="magnet" autocomplete="on"
								placeholder="请输入磁力链接,如：magnet:?xt=urn:btih:XXXXXXX"
								class="layui-input" value="">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">种子解析</label>
						<div class="layui-upload-drag" id="uploadTorrent">
							<i class="layui-icon"></i>
							<p>点击上传，或将文件拖拽到此处</p>
						</div>
					</div>
					<div class="layui-form-item">
						<div class="layui-input-block">
							<button class="layui-btn" lay-submit="" id="magnetBtn">立即提交</button>
							<button type="reset" class="layui-btn layui-btn-primary">重置</button>
						</div>
					</div>
				</form>

				<table class="layui-hide" lay-filter="magnetTable" id="magnetTable"></table>
			</div>

			<!-- 磁力搜索 -->
			<div class="layui-tab-item">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<label class="layui-form-label">搜索网站</label>
						<div class="layui-input-block">
							<select id="circuit" lay-verify="required" lay-search="">
								<option value="1" selected="selected">btstation</option>
								<option value="2">btyunsou</option>
								<option value="3">torrentba</option>
								<option value="4">anidex</option>
							</select>
						</div>
					</div>
				</form>
				<div class="layui-form-item">
					<label class="layui-form-label">关键字</label>
					<div class="layui-input-block">
						<input type="text" id="keyword" autocomplete="on"
							placeholder="请输入关键字" class="layui-input" value="阿丽塔">
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn" lay-submit="" id="queryBtn">立即提交</button>
						<button type="reset" class="layui-btn layui-btn-primary">重置</button>
					</div>
				</div>
				<table class="layui-hide" lay-filter="queryTable" id="queryTable"></table>
			</div>
		</div>
	</div>
	<script type="text/html" id="barDemo">
  		<a class="layui-btn layui-btn-xs" lay-event="parseMagnet">下载</a>
		<a class="layui-btn layui-btn-xs" lay-event="play">播放</a>
	</script>
	<script type="text/html" id="queryDemo">
  		<a class="layui-btn layui-btn-xs" lay-event="copy">复制磁力</a>
		<a class="layui-btn layui-btn-xs" lay-event="parseMagnet">用黑科云解析</a>
	</script>
	<script src="layui/layui.js"></script>
	<script>
		layui
				.use(
						[ 'form', 'upload', 'element', 'table' ],
						function() {
							var upload = layui.upload; //上传
							var element = layui.element; //元素操作
							var form = layui.form; //表单
							var $ = layui.jquery; //jq
							var table = layui.table; //table
							$("#magnetBtn").click(function() {
								layer.load(2);
								$.ajax({
									url : '/parseMagnet',
									type : "post",
									dataType : "json",
									data : {
										magnet : $("#magnet").val(),
									},
									async : false,
									success : function(data) {
										renderTable(data);
									}
								})
								layer.closeAll('loading');
								return false;
							});
							$("#queryBtn").click(function() {
								layer.load(2);
									$.ajax({
										url : '/magnetSearch',
										type : "post",
										dataType : "json",
										data : {
											circuit : $("#circuit").val(),
											keyword : $("#keyword").val(),
										},
										success : function(data) {
											queryTable(data);
											layer.closeAll('loading');
										}
									})
									layer.closeAll('loading');
								return false;
							});
							var renderTable = function(data) {
								table.render({
									elem : '#magnetTable',
									cols : [ [ {
										field : 'fileName',
										title : '文件名',
										minWidth : 120
									}, {
										field : 'fileSize',
										title : '文件大小',
										minWidth : 150
									}, {
										field : 'data',
										title : 'data',
										width : 80,
										hide : true
									}, {
										field : 's1',
										title : 's1',
										width : 80,
										hide : true
									}, {
										fixed : 'right',
										title : '操作',
										toolbar : '#barDemo',
										width : 150
									} ] ],
									data : data
								})
							}
							var queryTable = function(data) {
								table.render({
									elem : '#queryTable',
									cols : [ [ {
										field : 'fileName',
										title : '文件名',
										minWidth : 120
									}, {
										field : 'fileSize',
										title : '文件大小',
										minWidth : 30
									}, {
										field : 'magent',
										title : '磁力链接',
										minWidth : 150
									}, {
										fixed : 'right',
										title : '操作',
										toolbar : '#queryDemo',
										width : 250
									} ] ],
									data : data
								})
							}
							table
									.on(
											'tool(magnetTable)',
											function(obj) {
												var data = obj.data;
												var layEvent = obj.event;
												if (layEvent === 'parseMagnet') {
													$
															.ajax({
																url : '/download',
																type : "post",
																dataType : "json",
																data : {
																	data : data.data,
																	s1 : data.s1
																},
																async : false,
																success : function(
																		data) {
																	if (data.downUrl != null) {
																		var new_obj = $("<input value='" + data.downUrl + "'>");
																		$(
																				'body')
																				.before(
																						new_obj);
																		new_obj
																				.select();
																		document
																				.execCommand("copy");
																		new_obj
																				.remove();
																		layer
																				.alert("下载地址：<br/>"
																						+ data.downUrl
																						+ "<br/><div style='text-align:center;color:red;'>已经成功复制到剪切板</div>")
																	} else {
																		layer
																				.alert("解析失败，请重试或者换个资源");
																	}
																}
															})
												} else if (layEvent === 'play') {
													layer.msg('这个技术还不会,会的大佬提供下代码啊');
												}
											})
							table
									.on(
											'tool(queryTable)',
											function(obj) {
												var data = obj.data;
												var layEvent = obj.event;
												if (layEvent === 'copy') {
													var new_obj = $("<input value='" + data.magent + "'>");
													$('body').before(new_obj);
													new_obj.select();
													document
															.execCommand("copy");
													new_obj.remove();
													layer.msg('复制成功');
												} else if (layEvent === 'parseMagnet') {
													element.tabChange(
															'tabDemo',
															'heikeyunTab');
													$("#magnet").val(data.magent);
													$("#magnetBtn").click();
												}
											})
							upload.render({
								elem : '#uploadTorrent',
								url : '/uploadTorrent/',
								done : function(data) {
									renderTable(data);
								},
								accept : 'file'
							});
						});
	</script>
</body>